package com.demomaster.quickaccessibility.plugin;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import com.demomaster.quickaccessibility.core.QuickAccessibility;

public class PluginManager {
    private static final String TAG = "PluginManager";
    private Map<String, Plugin> plugins = new HashMap<>();
    private Map<String, List<Plugin>> listeners = new HashMap<>();
    static PluginManager instance;

    public static PluginManager getInstance() {
        if(instance==null){
            instance = new PluginManager();
        }
        return instance;
    }
    private PluginManager() {
    }
    public void registerPlugin(Plugin IPlugin) {
        plugins.put(IPlugin.getPluginId(), IPlugin);
        Log.d(TAG, "注册插件：" + IPlugin.getPluginId());
    }

    public void unregisterPlugin(Plugin IPlugin) {
        plugins.remove(IPlugin.getPluginId());
        Log.d(TAG, "卸载插件：" + IPlugin.getPluginId());
    }

//    public void addListener(String eventType, AccessibilityPlugin plugin) {
//        List<AccessibilityPlugin> eventListeners = listeners.computeIfAbsent(eventType, k -> new ArrayList<>());
//        eventListeners.add(plugin);
//        Log.d(TAG, "插件 " + plugin.getClass().getName() + " 注册了事件类型：" + eventType);
//    }
//
//    public void removeListener(String eventType, AccessibilityPlugin plugin) {
//        List<AccessibilityPlugin> eventListeners = listeners.get(eventType);
//        if (eventListeners != null) {
//            eventListeners.remove(plugin);
//            Log.d(TAG, "插件 " + plugin.getClass().getName() + " 从事件类型 " + eventType + " 中移除");
//        }
//    }

    public void dispatchEvent(AccessibilityEvent event) {
        String packageName = QuickAccessibility.getInstance().getCurrentPackageName();
        String currentActivityName = QuickAccessibility.getInstance().getCurrentActivityName();
        for(Plugin plugin : plugins.values()){
            plugin.onDispatchEvent(event,packageName,currentActivityName);
        }
    }

    public Map<String, Plugin> getPlugins() {
        return plugins;
    }
}